/**
 * 项目名称: Topic_430.cpp
 * 项目路径: study/DataStructure/LinkedList/LeetCode/链表/中等
 * 编辑时间: 2024年07月09日16时59分53秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int val;
    Node *prev;
    Node *next;
    Node *child;
};

class Solution {
public:
    vector<Node *>vn;
    Node * end(Node * head){
        Node * p = head;
        while (p->next)p=p->next;
        return p;
    }
    Node *flatten(Node *head) {
        if (!head)return head;

        Node * p =head;

        while (p){
            cout<<p->next<<" "<<p->val<<" "<<p->child<<endl;
            if(p->child){
                cout<<p->child<<" ";
                if(p->next)
                    vn.push_back(p->next);
                p->child->prev = p;
                p->next=p->child;
                p->child = nullptr;
            }
            p=p->next;
        }

        cout<<vn.size();
        for(int i=vn.size()-1;i>=0;i--){
            Node * op = end(head);
            op->next = vn[i];
            vn[i]->prev = op;
        }

        return head;
    }
};

int main() {

    return 0;
}
